<?php
require 'common/Utility.php';
require 'crud/CustomSecurity.class.php';
if(!isBrowserSupported()){
		header("Location: supportedBrowsers.php");
	} 
if ($_POST != null){
	require 'crud/Users.class.php';
	require 'db/configuration.php';
		$status = validateData();
	if (strcasecmp($status['msg'],'OK') == 0){
		$crud = new Users(con());
		if ($crud->add(escape_string($_POST['email']), md5(escape_string($_POST['pwd'])),'new',escape_string($_POST['fName']))){
			header("Location: register-complete.php");
		} 		
	} 
}?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>


<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<link rel="stylesheet" type="text/css" href="css/register.css" />
<link rel="stylesheet" type="text/css" href="css/page.css" />
<title>Register new user</title>
</head>
<body>

<?php include("common/lenta.php"); ?>

<div id="main">


<?php
global $status;
if ($status != null){
	echo $status['msg'];
}
?>
<div id="register">





<div id="form-container">

<div class="message">
	<p class="large">Попълнете формата за регистрация:</p>
</div>

<form action="register.php" method="post">

<ol>

	<li>
		<label for="email">email</label> 
		<input class="<?= getClassValue('email') ?>" type="text" name="email" value="<?= getPost('email')?>">
	</li>
	
	<li>
		<label for="pwd">парола</label> 
		<input class="password" class="<?= getClassValue('pwd')?>" type="password" name="pwd" size="15" maxlength="14">
	</li>
	
	<li>
		<label for="pwd2">повтори паролата</label> 
		<input class="password" class="<?= getClassValue('pwd2')?>" type="password" name="pwd2" size="15" maxlength="14">
	</li>
	
	<li>
		<label for="fName">име</label> 
		<input class="<?= getClassValue('name')?>" type="text" name="fName" value="<?= getPost('fName')?>">
	</li>
	
	<li>
	<?php
	$sq = generateSecurityQuestion(); 
	?>
		<label for="sq"><?= $sq['txt'] ?></label> 
		<input class="check" class="<?= getClassValue('sq')?>" type="text" name="sq" value="">
		<input type="hidden" name="answr" value="<?=$sq['r'] ?>">
	</li>
	
    <li><input id="submit" type="submit" name="submit" value=""></li>

</ol>

</form>
</div>

</div>
</div>


<?php
function validateData(){
	$status['msg'] = '';
	foreach ($_POST as $key => $value){

		if (strlen(trim($value)) == 0 && strcasecmp($key,'submit') != 0){
			$status['msg'] = '<div class="message"> полето '.$status['msg'].getNormalName($key).' e празно</div>';
			$status[$key] = 'wrong';
		}
	}
	
	if (strcasecmp($_POST['answr'], $_POST['sq']) != 0){
		$status['msg'] = $status['msg'].'<div class="message">Сметни пак </div>';
		$status['sq'] = 'wrong';
	}

	if (strlen($status['msg']) == 0){
		$crud = new Users(con());
		$user = $crud->getUsers(escape_string($_POST['email']));
		if (count($user) > 0){
			$status['msg'] = '<div class="message">Потребител с подобен email съществува </div>';
			$status['email'] = 'wrong';
		}
		if (!stripos($_POST['email'], '@') || !stripos($_POST['email'],'.')){
			$status['msg'] = '<div class="message">Email -а не е валиден</div><br>';
			$status['email'] = 'wrong';
		}
		if (strcmp($_POST['pwd'], $_POST['pwd2']) != null){
			$status['msg'] = $status['msg'].'<div class="message">Паролите са различни.</div><br>';
			$status['pwd'] = 'wrong';
			$status['pwd2'] = 'wrong';
		}
	}

	if (strlen($status['msg']) == 0){
		$status['msg'] = 'OK';

	}

	return $status;
}

function getClassValue($key){
	global $status;
	if ($_POST != null){
	} else {
		return getNormalValue($key);
	}
	
	if ($status[$key] == null){
		return getNormalValue($key);
	} else {
		return $status[$key];
	}
}

function getNormalValue($key){
	if (strcasecmp($key, 'pwd') == 0 || strcasecmp($key, 'pwd2') == 0 ){
		return 'textfield-pwd';
	}
	if (strcasecmp($key, 'email') == 0){
		return 'textfield-email';
	}
	if (strcasecmp($key, 'name') == 0){
		return 'textfield-name';
	}
	
	if (strcasecmp($key, 'sq') == 0){
		return 'textfield-sq';
	}
}

function getNormalName($key){
	if (strcasecmp($key, 'pwd') == 0){
		return 'парола';
	}
	if(strcasecmp($key, 'pwd2') == 0){
		return 'повтори паролата';
	}
	if (strcasecmp($key, 'email') == 0){
		return 'email';
	}
	if (strcasecmp($key, 'name') == 0){
		return 'име';
	}
}


?>
</body>
</html>